package Secondpagh;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.MPPointF;
import com.gyf.immersionbar.ImmersionBar;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import SHU.water2.R;
import SHU.water2.Spinnerstyle;
import ThirdPagh.waterLevelList;

import static SHU.water2.loginin.connection;

public class waterLevelChart extends AppCompatActivity implements View.OnClickListener {
    private LineChart lc1;
    private LineChart lc2;
    private LineChart lc3;
    private LineChart lc4;
    private Spinner spinner1;
    private Spinner spinner2;
    private Spinner spinner3;
    private Spinner spinner4;
    private Spinner spinner5;
    private Spinner spinner6;
    private ArrayAdapter<String> arr_adapter4;
    private ArrayAdapter<String> arr_adapter5;
    private ArrayAdapter<String> arr_adapter6;

    private int chartNum = 1;
    private Handler handler;
    private String[] town;
    private String[] point;
    private String[] site;
    private HashMap<String, Object> item;
    private List<HashMap<String, Object>> data;
    private String sql = "select DISTINCT location_zhen from pump_site";
    private float outriver[];
    private float inriver[];
    String titleT1;
    String titleT3;

    @SuppressLint("HandlerLeak")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.waterlevelchart);
        ImmersionBar.with(this).statusBarColor(R.color.color).fitsSystemWindows(true).init();
        handler = new Handler() {
            //接收消息
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                int flag = (int) msg.obj;
                Description description = new Description();
                switch (flag) {
                    case 1:
                        spinerset();
                        break;
                    case 2:
                        arr_adapter6 = new Spinnerstyle(waterLevelChart.this, site);
                        spinner6.setAdapter(arr_adapter6);
                        break;
                    case 3:
                        loadData1();
                        break;
                    case 4:
                        description.setText(titleT1);//设置文本
                        description.setTextSize(18f); //设置文本大小
                        description.setTextColor(Color.BLACK);//设置文本颜色
                        //获取屏幕的中间位置
                        WindowManager wm = (WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE);
                        DisplayMetrics dm = new DisplayMetrics();
                        wm.getDefaultDisplay().getMetrics(dm);
                        float x = (dm.widthPixels / 4) + 300;
                        description.setPosition(x , 50);//设置文本的位置
                        lc1.setDescription(description);

                        setStyle2();
                        loadData2();
                        lc2.setVisibility(View.VISIBLE);
                        break;
                    case 5:
                        setStyle3();
                        loadData3();
                        lc3.setVisibility(View.VISIBLE);
                        break;
                    case 6:
                        description.setText(titleT3);//设置文本
                        description.setTextSize(18f); //设置文本大小
                        description.setTextColor(Color.BLACK);//设置文本颜色
                        //获取屏幕的中间位置
                        wm = (WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE);
                        dm = new DisplayMetrics();
                        wm.getDefaultDisplay().getMetrics(dm);
                        x = (dm.widthPixels / 4) + 300;
                        description.setPosition(x , 50);//设置文本的位置
                        lc3.setDescription(description);

                        setStyle4();
                        loadData4();
                        lc4.setVisibility(View.VISIBLE);
                        break;
                }

            }
        };

        connect(sql);
        click();
        lc1 = findViewById(R.id.lc1);//获取控件
        lc2 = findViewById(R.id.lc2);//获取控件
        lc3 = findViewById(R.id.lc3);//获取控件
        lc4 = findViewById(R.id.lc4);//获取控件
        lc2.setVisibility(View.GONE);
        lc3.setVisibility(View.GONE);
        lc4.setVisibility(View.GONE);
        //lc.notifyDataSetChanged();//刷新数据
        //lc.invalidate();//重绘

        setStyle1();
        //loadData1();

        MyMarkerView mv = new MyMarkerView(this);
        lc1.setMarker(mv);
        lc2.setMarker(mv);
        lc3.setMarker(mv);
        lc4.setMarker(mv);

    }

    private void click() {
        findViewById(R.id.ret1).setOnClickListener(this);
        findViewById(R.id.check1).setOnClickListener(this);
        findViewById(R.id.check2).setOnClickListener(this);
        spinner1 = findViewById(R.id.spinner1);
        spinner2 = findViewById(R.id.spinner2);
        spinner3 = findViewById(R.id.spinner3);
        spinner4 = findViewById(R.id.spinner4);
        spinner5 = findViewById(R.id.spinner5);
        spinner6 = findViewById(R.id.spinner6);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.check1:
                connect(sql);
                break;

            case R.id.check2:
                Intent intent = new Intent();
                intent.setClass(getApplicationContext(), waterLevelList.class);
                startActivity(intent);
                break;

            case R.id.ret1:
                this.finish();
                break;
        }
    }


    // * 设置为强制横屏
    @Override
    protected void onResume() {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        super.onResume();
    }

    public void setStyle1() {
        //图例
        Legend legend1 = lc1.getLegend();
        legend1.setDrawInside(false);//不绘制在图形内部
        legend1.setTextColor(Color.BLACK);//设置文字颜色
        legend1.setFormSize(10f);//设置图例的大小
        legend1.setTextSize(10f);

        //得到YAxis对象
        YAxis yAxis1 = lc1.getAxisLeft();
        YAxis yAxis2 = lc1.getAxisRight();
        lc1.getAxisRight().setEnabled(true);//不显示右侧的y轴

        //设置字体颜色白色
        yAxis1.setTextColor(Color.parseColor("#2D93F6"));
        //设置最大值和最小值
        yAxis1.setAxisMaximum(6);
        yAxis1.setAxisMinimum(-1);
        //设置字体大小
        yAxis1.setTextSize(10f);
        //字体加粗
        yAxis1.setTypeface(Typeface.DEFAULT_BOLD);
        //yAxis1.setGridColor(Color.WHITE);//网格颜色
        //强制只绘制5个Label
        yAxis1.setLabelCount(8, true);
        yAxis1.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                String tep = value + "";
                return tep.substring(0, tep.indexOf(".")) + "m";
            }
        });

        yAxis2.setTextColor(Color.parseColor("#FA8874"));
        //设置最大值和最小值
        yAxis2.setAxisMaximum(6);
        yAxis2.setAxisMinimum(-1);
        //设置字体大小
        yAxis2.setTextSize(10f);
        //字体加粗
        yAxis2.setTypeface(Typeface.DEFAULT_BOLD);
        //强制只绘制5个Label
        yAxis2.setLabelCount(8, true);
        yAxis2.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                String tep = value + "";
                return tep.substring(0, tep.indexOf(".")) + "m";
            }
        });

        //限制线
        LimitLine outlimit = new LimitLine(3.1f, "");
        outlimit.enableDashedLine(10f, 5f, 5f);//虚线
        //line.setLineColor(Color.parseColor("#FA8874"));
        outlimit.setLineColor(Color.parseColor("#2D93F6"));
        outlimit.setLineWidth(2f);

        LimitLine inlimit = new LimitLine(5.8f, "");
        inlimit.enableDashedLine(10f, 5f, 5f);//虚线
        inlimit.setLineColor(Color.parseColor("#FA8874"));
        inlimit.setLineWidth(2f);

        yAxis1.addLimitLine(outlimit);
        yAxis2.addLimitLine(inlimit);

        //得到XAxis对象
        XAxis xAxis = lc1.getXAxis();
        xAxis.setTextColor(Color.BLACK);//字体颜色
        //设置最大最小值
        xAxis.setAxisMaximum(23);
        xAxis.setAxisMinimum(0);
        xAxis.setGranularity(0);//设置间隔
        //将x显示在下方
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setTextSize(10f);//字体大小
        xAxis.setLabelCount(24);
        xAxis.setDrawGridLines(false);
    }

    public void setStyle2() {

        //图例
        Legend legend = lc2.getLegend();
        legend.setDrawInside(false);//不绘制在图形内部
        legend.setTextColor(Color.BLACK);//设置文字颜色
        legend.setFormSize(10f);//设置图例的大小
        legend.setTextSize(10f);

        //得到YAxis对象
        YAxis yAxis1 = lc2.getAxisLeft();
        YAxis yAxis2 = lc2.getAxisRight();
        lc2.getAxisRight().setEnabled(true);//不显示右侧的y轴

        //设置字体颜色白色
        yAxis1.setTextColor(Color.parseColor("#2D93F6"));
        //设置最大值和最小值
        yAxis1.setAxisMaximum(6);
        yAxis1.setAxisMinimum(-1);
        //设置字体大小
        yAxis1.setTextSize(10f);
        //字体加粗
        yAxis1.setTypeface(Typeface.DEFAULT_BOLD);
        //yAxis1.setGridColor(Color.WHITE);//网格颜色
        //强制只绘制5个Label
        yAxis1.setLabelCount(8, true);
        yAxis1.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                String tep = value + "";
                return tep.substring(0, tep.indexOf(".")) + "m";
            }
        });

        yAxis2.setTextColor(Color.parseColor("#FA8874"));
        //设置最大值和最小值
        yAxis2.setAxisMaximum(6);
        yAxis2.setAxisMinimum(-1);
        //设置字体大小
        yAxis2.setTextSize(10f);
        //字体加粗
        yAxis2.setTypeface(Typeface.DEFAULT_BOLD);
        //强制只绘制5个Label
        yAxis2.setLabelCount(8, true);
        yAxis2.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                String tep = value + "";
                return tep.substring(0, tep.indexOf(".")) + "m";
            }
        });

        //限制线
        LimitLine outlimit = new LimitLine(3.1f, "");
        outlimit.enableDashedLine(10f, 5f, 5f);//虚线
        //line.setLineColor(Color.parseColor("#FA8874"));
        outlimit.setLineColor(Color.parseColor("#2D93F6"));
        outlimit.setLineWidth(2f);

        LimitLine inlimit = new LimitLine(5.8f, "");
        inlimit.enableDashedLine(10f, 5f, 5f);//虚线
        inlimit.setLineColor(Color.parseColor("#FA8874"));
        inlimit.setLineWidth(2f);

        yAxis1.addLimitLine(outlimit);
        yAxis2.addLimitLine(inlimit);

        //得到XAxis对象
        XAxis xAxis = lc1.getXAxis();
        xAxis.setTextColor(Color.BLACK);//字体颜色
        //设置最大最小值
        xAxis.setAxisMaximum(23);
        xAxis.setAxisMinimum(0);
        xAxis.setGranularity(0);//设置间隔
        //将x显示在下方
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setTextSize(10f);//字体大小
        xAxis.setLabelCount(24);
        xAxis.setDrawGridLines(false);
    }

    public void setStyle3() {
        //图例
        Legend legend = lc3.getLegend();
        legend.setDrawInside(false);//不绘制在图形内部
        legend.setTextColor(Color.BLACK);//设置文字颜色
        legend.setFormSize(10f);//设置图例的大小
        legend.setTextSize(10f);

        //得到YAxis对象
        YAxis yAxis1 = lc3.getAxisLeft();
        YAxis yAxis2 = lc3.getAxisRight();
        lc3.getAxisRight().setEnabled(true);//不显示右侧的y轴

        //设置字体颜色白色
        yAxis1.setTextColor(Color.parseColor("#2D93F6"));
        //设置最大值和最小值
        yAxis1.setAxisMaximum(6);
        yAxis1.setAxisMinimum(-1);
        //设置字体大小
        yAxis1.setTextSize(10f);
        //字体加粗
        yAxis1.setTypeface(Typeface.DEFAULT_BOLD);
        //yAxis1.setGridColor(Color.WHITE);//网格颜色
        //强制只绘制5个Label
        yAxis1.setLabelCount(8, true);
        yAxis1.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                String tep = value + "";
                return tep.substring(0, tep.indexOf(".")) + "m";
            }
        });

        yAxis2.setTextColor(Color.parseColor("#FA8874"));
        //设置最大值和最小值
        yAxis2.setAxisMaximum(6);
        yAxis2.setAxisMinimum(-1);
        //设置字体大小
        yAxis2.setTextSize(10f);
        //字体加粗
        yAxis2.setTypeface(Typeface.DEFAULT_BOLD);
        //强制只绘制5个Label
        yAxis2.setLabelCount(8, true);
        yAxis2.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                String tep = value + "";
                return tep.substring(0, tep.indexOf(".")) + "m";
            }
        });

        //限制线
        LimitLine outlimit = new LimitLine(3.1f, "");
        outlimit.enableDashedLine(10f, 5f, 5f);//虚线
        //line.setLineColor(Color.parseColor("#FA8874"));
        outlimit.setLineColor(Color.parseColor("#2D93F6"));
        outlimit.setLineWidth(2f);

        LimitLine inlimit = new LimitLine(5.8f, "");
        inlimit.enableDashedLine(10f, 5f, 5f);//虚线
        inlimit.setLineColor(Color.parseColor("#FA8874"));
        inlimit.setLineWidth(2f);

        yAxis1.addLimitLine(outlimit);
        yAxis2.addLimitLine(inlimit);

        //得到XAxis对象
        XAxis xAxis = lc4.getXAxis();
        xAxis.setTextColor(Color.BLACK);//字体颜色
        //设置最大最小值
        xAxis.setAxisMaximum(23);
        xAxis.setAxisMinimum(0);
        xAxis.setGranularity(0);//设置间隔
        //将x显示在下方
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setTextSize(10f);//字体大小
        xAxis.setLabelCount(24);
        xAxis.setDrawGridLines(false);
    }

    public void setStyle4() {
        //图例
        Legend legend = lc4.getLegend();
        legend.setDrawInside(false);//不绘制在图形内部
        legend.setTextColor(Color.BLACK);//设置文字颜色
        legend.setFormSize(10f);//设置图例的大小
        legend.setTextSize(10f);

        //得到YAxis对象
        YAxis yAxis1 = lc4.getAxisLeft();
        YAxis yAxis2 = lc4.getAxisRight();
        lc4.getAxisRight().setEnabled(true);//不显示右侧的y轴

        //设置字体颜色白色
        yAxis1.setTextColor(Color.parseColor("#2D93F6"));
        //设置最大值和最小值
        yAxis1.setAxisMaximum(6);
        yAxis1.setAxisMinimum(-1);
        //设置字体大小
        yAxis1.setTextSize(10f);
        //字体加粗
        yAxis1.setTypeface(Typeface.DEFAULT_BOLD);
        //yAxis1.setGridColor(Color.WHITE);//网格颜色
        //强制只绘制5个Label
        yAxis1.setLabelCount(8, true);
        yAxis1.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                String tep = value + "";
                return tep.substring(0, tep.indexOf(".")) + "m";
            }
        });

        yAxis2.setTextColor(Color.parseColor("#FA8874"));
        //设置最大值和最小值
        yAxis2.setAxisMaximum(6);
        yAxis2.setAxisMinimum(-1);
        //设置字体大小
        yAxis2.setTextSize(10f);
        //字体加粗
        yAxis2.setTypeface(Typeface.DEFAULT_BOLD);
        //强制只绘制5个Label
        yAxis2.setLabelCount(8, true);
        yAxis2.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                String tep = value + "";
                return tep.substring(0, tep.indexOf(".")) + "m";
            }
        });

        //限制线
        LimitLine outlimit = new LimitLine(3.1f, "");
        outlimit.enableDashedLine(10f, 5f, 5f);//虚线
        //line.setLineColor(Color.parseColor("#FA8874"));
        outlimit.setLineColor(Color.parseColor("#2D93F6"));
        outlimit.setLineWidth(2f);

        LimitLine inlimit = new LimitLine(5.8f, "");
        inlimit.enableDashedLine(10f, 5f, 5f);//虚线
        inlimit.setLineColor(Color.parseColor("#FA8874"));
        inlimit.setLineWidth(2f);

        yAxis1.addLimitLine(outlimit);
        yAxis2.addLimitLine(inlimit);

        //得到XAxis对象
        XAxis xAxis = lc4.getXAxis();
        xAxis.setTextColor(Color.BLACK);//字体颜色
        //设置最大最小值
        xAxis.setAxisMaximum(23);
        xAxis.setAxisMinimum(0);
        xAxis.setGranularity(0);//设置间隔
        //将x显示在下方
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setTextSize(10f);//字体大小
        xAxis.setLabelCount(24);
        xAxis.setDrawGridLines(false);
    }

    private void connect(String sql) {
        //新建线程连接数据库，防止主界面卡死
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Statement statement = connection.createStatement();
                    if (sql.equals("select DISTINCT location_zhen from pump_site")) {
                        ArrayList<String> L1 = new ArrayList<>();
                        ArrayList<String> L2 = new ArrayList<>();
                        // 创建用来执行sql语句的对象
                        String sql1 = "select DISTINCT location_zhen from pump_site";
                        String sql2 = "select DISTINCT location_point from pump_site";
                        // 执行sql查询语句并获取查询信息
                        ResultSet rSet1 = statement.executeQuery(sql1);
                        statement = connection.createStatement();
                        ResultSet rSet2 = statement.executeQuery(sql2);

                        while (rSet1.next()) {
                            L1.add(rSet1.getString("location_zhen"));
                        }
                        while (rSet2.next()) {
                            L2.add(rSet2.getString("location_point"));
                        }

                        town = new String[L1.size()];
                        point = new String[L2.size()];
                        for (int i = 0; i < L1.size(); i++) {
                            town[i] = (String) L1.get(i);
                        }
                        for (int i = 0; i < L2.size(); i++) {
                            point[i] = (String) L2.get(i);
                        }

                        Message msg = new Message();
                        msg.obj = 1;
                        handler.sendMessage(msg);
                    }

                    if (sql.startsWith("select s", 0)) {
                        ArrayList<String> L1 = new ArrayList<>();
                        // 创建用来执行sql语句的对象
                        String sql1 = sql;
                        // 执行sql查询语句并获取查询信息
                        ResultSet rSet1 = statement.executeQuery(sql1);

                        while (rSet1.next()) {
                            L1.add(rSet1.getString("site_name"));
                        }

                        site = new String[L1.size()];
                        for (int i = 0; i < L1.size(); i++) {
                            site[i] = (String) L1.get(i);
                        }

                        Message msg = new Message();
                        msg.obj = 2;
                        handler.sendMessage(msg);
                    }

                    if (sql.startsWith("site_name=", 0)) {
                        ArrayList<String> L1 = new ArrayList<>();
                        ArrayList<String> L2 = new ArrayList<>();
                        // 创建用来执行sql语句的对象
                        String sql1 = "select out_level from water_level1 where site_name = '" + sql.substring(10) + "'";
                        ResultSet rSet1 = statement.executeQuery(sql1);
                        String sql2 = "select in_level from water_level1 where site_name = '" + sql.substring(10) + "'";
                        statement = connection.createStatement();
                        ResultSet rSet2 = statement.executeQuery(sql2);

                        while (rSet1.next()) {
                            L1.add(rSet1.getString("out_level"));
                            //L2.add(rSet2.getString("in_level"));
                        }
                        while (rSet2.next()) {
                            L2.add(rSet2.getString("in_level"));
                        }

                        outriver = new float[L1.size()];
                        inriver = new float[L2.size()];
                        for (int i = 0; i < L1.size(); i++) {
                            outriver[i] = Float.parseFloat((String) L1.get(i));
                            inriver[i] = Float.parseFloat((String) L2.get(i));
                        }

                        Message msg = new Message();
                        if (chartNum == 1) {
                            msg.obj = chartNum + 2;
                            chartNum++;
                        } else if (chartNum == 2) {
                            msg.obj = chartNum + 2;
                            chartNum++;
                        } else if (chartNum == 3) {
                            msg.obj = chartNum + 2;
                            chartNum++;
                        } else if (chartNum == 4) {
                            msg.obj = chartNum + 2;
                            chartNum++;
                        }
                        handler.sendMessage(msg);
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void loadData1() {
        Description description = new Description();
        titleT1 = sql.substring(10) + "24小时水位";
        description.setText(titleT1);//设置文本
        description.setTextSize(18f); //设置文本大小
        description.setTextColor(Color.BLACK);//设置文本颜色
        //获取屏幕的中间位置
        WindowManager wm = (WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        float x = (dm.widthPixels / 2) + 300;
        description.setPosition(x, 50);//设置文本的位置
        lc1.setDescription(description);
        //设置边距
        lc1.setExtraOffsets(30, 60, 30, 10);

        //不绘制图例和标题
        //lc.getLegend().setEnabled(false);
        //lc.getDescription().setEnabled(false);

        List<Entry> yData1 = new ArrayList<Entry>();
        List<Entry> yData2 = new ArrayList<Entry>();

        for (int i = 0; i < inriver.length; i++) {
            yData1.add(new Entry(i, outriver[i]));
            yData2.add(new Entry(i, inriver[i]));
        }


        LineDataSet lineDataSet1 = new LineDataSet(yData1, "外河水位");
        LineDataSet lineDataSet2 = new LineDataSet(yData2, "内河水位");

        lineDataSet1.setLineWidth(3);//设置线条宽度
        lineDataSet1.setColor(Color.parseColor("#2D93F6"));//设置线条颜色
        lineDataSet1.setDrawCircles(false);
        lineDataSet1.setDrawValues(false);//不绘制文本
        lineDataSet1.setMode(LineDataSet.Mode.CUBIC_BEZIER);//设置为贝塞尔曲线
        lineDataSet1.setCubicIntensity(0.05f);//曲线强度0.1
        lineDataSet1.setHighLightColor(Color.RED);//设置点击交点后显示交高亮线的颜色
        lineDataSet1.setHighlightLineWidth(1f);

        lineDataSet2.setLineWidth(3);//设置线条宽度
        lineDataSet2.setColor(Color.parseColor("#FA8874"));//设置线条颜色
        lineDataSet2.setDrawCircles(false);
        lineDataSet2.setDrawValues(false);//不绘制文本
        lineDataSet2.setMode(LineDataSet.Mode.CUBIC_BEZIER);//设置为贝塞尔曲线
        lineDataSet2.setCubicIntensity(0.05f);//曲线强度0.1
        lineDataSet2.setHighLightColor(Color.RED);//设置点击交点后显示交高亮线的颜色
        lineDataSet2.setHighlightLineWidth(1f);

        LineData lineData = new LineData(lineDataSet1, lineDataSet2);

        lc1.setData(lineData);
        lc1.setScaleEnabled(true);// 是否可以缩放
        lc1.setDoubleTapToZoomEnabled(false);//设置是否可以通过双击屏幕放大图表。
    }

    private void loadData2() {
        Description description = new Description();
        description.setText(sql.substring(10) + "24小时水位");//设置文本
        description.setTextSize(18f); //设置文本大小
        description.setTextColor(Color.BLACK);//设置文本颜色
        //获取屏幕的中间位置
        WindowManager wm = (WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        float x = (dm.widthPixels / 4) +300;
        description.setPosition(x , 50);//设置文本的位置
        lc2.setDescription(description);
        //设置边距
        lc2.setExtraOffsets(30, 60, 30, 10);
        //不绘制图例和标题
        //lc.getLegend().setEnabled(false);
        //lc.getDescription().setEnabled(false);

        List<Entry> yData1 = new ArrayList<Entry>();
        List<Entry> yData2 = new ArrayList<Entry>();

        for (int i = 0; i < inriver.length; i++) {
            yData1.add(new Entry(i, outriver[i]));
            yData2.add(new Entry(i, inriver[i]));
        }


        LineDataSet lineDataSet1 = new LineDataSet(yData1, "外河水位");
        LineDataSet lineDataSet2 = new LineDataSet(yData2, "内河水位");

        lineDataSet1.setLineWidth(3);//设置线条宽度
        lineDataSet1.setColor(Color.parseColor("#2D93F6"));//设置线条颜色
        lineDataSet1.setDrawCircles(false);
        lineDataSet1.setDrawValues(false);//不绘制文本
        lineDataSet1.setMode(LineDataSet.Mode.CUBIC_BEZIER);//设置为贝塞尔曲线
        lineDataSet1.setCubicIntensity(0.05f);//曲线强度0.1
        lineDataSet1.setHighLightColor(Color.RED);//设置点击交点后显示交高亮线的颜色
        lineDataSet1.setHighlightLineWidth(1f);

        lineDataSet2.setLineWidth(3);//设置线条宽度
        lineDataSet2.setColor(Color.parseColor("#FA8874"));//设置线条颜色
        lineDataSet2.setDrawCircles(false);
        lineDataSet2.setDrawValues(false);//不绘制文本
        lineDataSet2.setMode(LineDataSet.Mode.CUBIC_BEZIER);//设置为贝塞尔曲线
        lineDataSet2.setCubicIntensity(0.05f);//曲线强度0.1
        lineDataSet2.setHighLightColor(Color.RED);//设置点击交点后显示交高亮线的颜色
        lineDataSet2.setHighlightLineWidth(1f);

        LineData lineData = new LineData(lineDataSet1, lineDataSet2);

        lc2.setData(lineData);
        lc2.setScaleEnabled(true);// 是否可以缩放
        lc2.setDoubleTapToZoomEnabled(false);//设置是否可以通过双击屏幕放大图表。
    }

    private void loadData3() {
        Description description = new Description();
        titleT3 = sql.substring(10) + "24小时水位";
        description.setText(titleT3);//设置文本
        description.setTextSize(18f); //设置文本大小
        description.setTextColor(Color.BLACK);//设置文本颜色
        //获取屏幕的中间位置
        WindowManager wm = (WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        float x = (dm.widthPixels / 2) + 300;
        description.setPosition(x, 50);//设置文本的位置
        lc3.setDescription(description);
        //设置边距
        lc3.setExtraOffsets(30, 60, 30, 10);
        //不绘制图例和标题
        //lc.getLegend().setEnabled(false);
        //lc.getDescription().setEnabled(false);

        List<Entry> yData1 = new ArrayList<Entry>();
        List<Entry> yData2 = new ArrayList<Entry>();

        for (int i = 0; i < inriver.length; i++) {
            yData1.add(new Entry(i, outriver[i]));
            yData2.add(new Entry(i, inriver[i]));
        }


        LineDataSet lineDataSet1 = new LineDataSet(yData1, "外河水位");
        LineDataSet lineDataSet2 = new LineDataSet(yData2, "内河水位");

        lineDataSet1.setLineWidth(3);//设置线条宽度
        lineDataSet1.setColor(Color.parseColor("#2D93F6"));//设置线条颜色
        lineDataSet1.setDrawCircles(false);
        lineDataSet1.setDrawValues(false);//不绘制文本
        lineDataSet1.setMode(LineDataSet.Mode.CUBIC_BEZIER);//设置为贝塞尔曲线
        lineDataSet1.setCubicIntensity(0.05f);//曲线强度0.1
        lineDataSet1.setHighLightColor(Color.RED);//设置点击交点后显示交高亮线的颜色
        lineDataSet1.setHighlightLineWidth(1f);

        lineDataSet2.setLineWidth(3);//设置线条宽度
        lineDataSet2.setColor(Color.parseColor("#FA8874"));//设置线条颜色
        lineDataSet2.setDrawCircles(false);
        lineDataSet2.setDrawValues(false);//不绘制文本
        lineDataSet2.setMode(LineDataSet.Mode.CUBIC_BEZIER);//设置为贝塞尔曲线
        lineDataSet2.setCubicIntensity(0.05f);//曲线强度0.1
        lineDataSet2.setHighLightColor(Color.RED);//设置点击交点后显示交高亮线的颜色
        lineDataSet2.setHighlightLineWidth(1f);

        LineData lineData = new LineData(lineDataSet1, lineDataSet2);

        lc3.setData(lineData);
        lc3.setScaleEnabled(true);// 是否可以缩放
        lc3.setDoubleTapToZoomEnabled(false);//设置是否可以通过双击屏幕放大图表。
    }

    private void loadData4() {
        Description description = new Description();
        description.setText(sql.substring(10) + "24小时水位");//设置文本
        description.setTextSize(18f); //设置文本大小
        description.setTextColor(Color.BLACK);//设置文本颜色
        //获取屏幕的中间位置
        WindowManager wm = (WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        float x = (dm.widthPixels / 4)+300;
        description.setPosition(x , 50);//设置文本的位置

        lc4.setDescription(description);//添加给LineChart
        //设置边距
        lc4.setExtraOffsets(30, 60, 30, 10);
        //不绘制图例和标题
        //lc.getLegend().setEnabled(false);
        //lc.getDescription().setEnabled(false);

        List<Entry> yData1 = new ArrayList<Entry>();
        List<Entry> yData2 = new ArrayList<Entry>();

        for (int i = 0; i < inriver.length; i++) {
            yData1.add(new Entry(i, outriver[i]));
            yData2.add(new Entry(i, inriver[i]));
        }


        LineDataSet lineDataSet1 = new LineDataSet(yData1, "外河水位");
        LineDataSet lineDataSet2 = new LineDataSet(yData2, "内河水位");

        lineDataSet1.setLineWidth(3);//设置线条宽度
        lineDataSet1.setColor(Color.parseColor("#2D93F6"));//设置线条颜色
        lineDataSet1.setDrawCircles(false);
        lineDataSet1.setDrawValues(false);//不绘制文本
        lineDataSet1.setMode(LineDataSet.Mode.CUBIC_BEZIER);//设置为贝塞尔曲线
        lineDataSet1.setCubicIntensity(0.05f);//曲线强度0.1
        lineDataSet1.setHighLightColor(Color.RED);//设置点击交点后显示交高亮线的颜色
        lineDataSet1.setHighlightLineWidth(1f);

        lineDataSet2.setLineWidth(3);//设置线条宽度
        lineDataSet2.setColor(Color.parseColor("#FA8874"));//设置线条颜色
        lineDataSet2.setDrawCircles(false);
        lineDataSet2.setDrawValues(false);//不绘制文本
        lineDataSet2.setMode(LineDataSet.Mode.CUBIC_BEZIER);//设置为贝塞尔曲线
        lineDataSet2.setCubicIntensity(0.05f);//曲线强度0.1
        lineDataSet2.setHighLightColor(Color.RED);//设置点击交点后显示交高亮线的颜色
        lineDataSet2.setHighlightLineWidth(1f);

        LineData lineData = new LineData(lineDataSet1, lineDataSet2);

        lc4.setData(lineData);
        lc4.setScaleEnabled(true);// 是否可以缩放
        lc4.setDoubleTapToZoomEnabled(false);//设置是否可以通过双击屏幕放大图表。
    }

    //自定义标记
    public class MyMarkerView extends MarkerView {

        private TextView tvContent;
        private DecimalFormat format = new DecimalFormat("##0");

        public MyMarkerView(Context context) {
            super(context, R.layout.markview);
            tvContent = (TextView) findViewById(R.id.MarkContent);
        }

        @Override
        public void refreshContent(Entry e, Highlight highlight) {

            tvContent.setText(format.format(e.getX()) + ":00" + "   " + format.format(e.getY()) + "m");

            super.refreshContent(e, highlight);
        }

        @Override
        public MPPointF getOffset() {
            return new MPPointF(-(getWidth() / 2), -getHeight());
        }

    }


    public void spinerset() {
        String[] items1 = getResources().getStringArray(R.array.year);
        String[] items2 = getResources().getStringArray(R.array.months);
        String[] items3 = getResources().getStringArray(R.array.days);
        String[] items6 = getResources().getStringArray(R.array.site);


        //适配器1
        ArrayAdapter<String> arr_adapter1 = new Spinnerstyle(this, items1);
        //设置样式
        arr_adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner1.setAdapter(arr_adapter1);

        //适配器2
        ArrayAdapter<String> arr_adapter2 = new Spinnerstyle(this, items2);
        //设置样式
        arr_adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner2.setAdapter(arr_adapter2);

        //适配器3
        ArrayAdapter<String> arr_adapter3 = new Spinnerstyle(this, items3);
        //设置样式
        arr_adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner3.setAdapter(arr_adapter3);

        //适配器3
        arr_adapter4 = new Spinnerstyle(this, town);
        //设置样式
        arr_adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner4.setAdapter(arr_adapter4);
        spinner4.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //String spinnerS4 = arr_adapter4.getItem(position).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        //适配器3
        arr_adapter5 = new Spinnerstyle(this, point);
        //设置样式
        arr_adapter5.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner5.setAdapter(arr_adapter5);
        spinner5.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String spinnerS5 = arr_adapter5.getItem(position).toString();
                sql = "select site_name from pump_site where location_point =  '" + spinnerS5 + "'";
                connect(sql);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        //适配器3
        arr_adapter6 = new Spinnerstyle(this, items6);
        //设置样式
        arr_adapter6.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner6.setAdapter(arr_adapter6);
        spinner6.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                sql = "site_name=" + arr_adapter6.getItem(position).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }
}







